package com.snowflake.ecatalog.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.collections.list.LazyList;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.snowflake.ecatalog.contant.Status;

/**
 * 企业
 */
// JPA标识
@Entity
@Table(name = "t_ec_company")
public class Company extends OpEntity {

	private String number;
	private String name;
	private String remark;
	private int status;
	private List<CompanyCategory> companyCategorys;
	private List<CompanyLinkman> companyLinkmans;
	private List<Profile> profiles;

	@SuppressWarnings("unchecked")
	public Company() {
		super();
		companyCategorys = LazyList.decorate(new ArrayList<CompanyCategory>(),
				FactoryUtils.instantiateFactory(CompanyCategory.class));
		companyLinkmans = LazyList.decorate(new ArrayList<CompanyLinkman>(),
				FactoryUtils.instantiateFactory(CompanyLinkman.class));
	}

	public Company(Long id) {
		super();
		this.id = id;
	}

	@ManyToMany(cascade = CascadeType.REFRESH, mappedBy = "companys", fetch = FetchType.LAZY)
	public List<Profile> getProfiles() {
		return profiles;
	}

	public void setProfiles(List<Profile> profiles) {
		this.profiles = profiles;
	}

	@ManyToMany
	@Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE })
	@JoinTable(name = "t_ec_companycategorydetail", joinColumns = { @JoinColumn(name = "company_id") }, inverseJoinColumns = { @JoinColumn(name = "category_id") })
	// Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	public List<CompanyCategory> getCompanyCategorys() {
		return companyCategorys;
	}

	public void setCompanyCategorys(List<CompanyCategory> companyCategorys) {
		this.companyCategorys = companyCategorys;
	}

	@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "company", fetch = FetchType.EAGER)
	public List<CompanyLinkman> getCompanyLinkmans() {
		return companyLinkmans;
	}

	public void setCompanyLinkmans(List<CompanyLinkman> companyLinkmans) {
		this.companyLinkmans = companyLinkmans;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	// 不持久化到数据库
	@Transient
	public String getStatusName() {
		return Status.alias(this.status);
	}
}
